<template>
  <div class="messageContainer">
    <transition name="fade" @before-leave="onClose" @after-leave="onDestroy">
      <div
        :style="{
          top: topOffset + 'px',
          backgroundColor: 'white',
        }"
        class="message"
        v-if="visiable"
      >
        <img :src="require('./' + icon)" />
        {{ message }}
      </div>
    </transition>
  </div>
</template>

<script>
/* eslint-disable */
export default {
  props: {
    topOffset: {
      type: Number,
      required: true,
    },
    message: {
      type: String,
      required: true,
    },
    type: {
      type: String,
      default: "success",
    },
    duration: {
      type: Number,
      default: 2000,
    },
    id: {
      type: Number,
      required: true,
    },
    onDestroy: {
      type: Function,
      required: true,
    },
    onClose: {
      type: Function,
      required: true,
    },
  },
  data() {
    return {
      icon: "勾.png",
      visiable: false,
    };
  },
  methods: {
    close() {
      this.visiable = false;
    },
  },
  mounted() {
    this.icon = this.type === "success" ? "勾.png" : "叉.png";
    this.visiable = true;
    setTimeout(() => {
      this.close();
    }, this.duration);
  },
};
</script>

<style scoped>
.messageContainer {
  position: relative;
  width: 100%; /* 容器宽度占据整个屏幕宽度 */
  height: 100vh; /* 容器高度占据整个屏幕高度 */
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}

.message {
  position: fixed;
  transform: translate(0%, 15px);
  padding: 10px;
  width: 300px;
  z-index: 999999;
  border-radius: 5px;
  font-size: 110%;
  text-align: center;
  border: #3f69e4 2px solid;
}

img {
  width: 15px;
  margin-top: -2px;
}

.fade-enter-active,
.fade-leave-active {
  transition: transform 0.4s, opacity 0.4s;
}

.fade-enter-from {
  transform: translate(0, -15px);
  opacity: 0;
}

.fade-leave-to {
  transform: translate(0, -15px) !important;
  opacity: 0;
}
</style>
